<template>
    <div class="app-container">
        <el-table
            :data="data"
            style="width: 100%;"
            border
            v-loading="listLoading"
        >
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="name" label="项目名" width="150" align="center"></el-table-column>
            <el-table-column prop="description" label="描述" align="center">
                <template slot-scope="scope">
                    {{ scope.row.description.toLocaleString() }}
                </template>
            </el-table-column>
            <el-table-column label="预览图" width="180" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 100px;"
                        :src="scope.row.thumb"
                        fit="fill"
                        :preview-src-list="srcList"
                    ></el-image>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <a :href="scope.row.url" target="_blank" style="margin-right: 10px">
                        <el-button type="warning" icon="el-icon-star-on" size="mini" circle></el-button>
                    </a>
                    <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="editProject(scope.row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="delProject(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>


        <el-dialog
            title="编辑项目"
            :visible.sync="dialogVisible"
            fullscreen
        >
            <el-form :mode="form">
                <el-form-item label="项目名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
                <el-form-item label="项目链接">
                    <el-input v-model="form.url"></el-input>
                </el-form-item>
                <el-form-item label="github地址">
                    <el-input v-model="form.github"></el-input>
                </el-form-item>
                <el-form-item label="预览图">
                    <Upload v-model="form.thumb"></Upload>
                </el-form-item>
                <el-form-item label="排序">
                    <el-select v-model="form.order">
                        <el-option label="1" value="1"></el-option>
                        <el-option label="2" value="2"></el-option>
                        <el-option label="3" value="3"></el-option>
                        <el-option label="4" value="4"></el-option>
                        <el-option label="5" value="5"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getProject, setProject, delProject, addProject } from '@/api/project'
import {server_URL} from '@/urlConfig'
import Upload from '@/components/Upload'

export default {
    name: 'Projects',
    components: {
        Upload
    },
    data() {
        return {
            data: [],
            total: 0,
            listLoading: false,
            srcList: [],
            dialogVisible: false,
            form: {
                name: '',
                description: '',
                url: '',
                github: '',
                thumb: '',
                order: 1
            }
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        fetchData() {
            this.listLoading = true
            getProject().then(res => {
                this.listLoading = false
                this.data = res.data
                this.total = res.data.length
                for(let item of this.data){
                    // item.thumb2 = server_URL + item.thumb
                    this.srcList.push(item.thumb)
                }
                // console.log('项目',this.data)
            })
        },
        editProject(projectInfo) {
            // console.log('编辑项目',projectInfo)
            this.form = projectInfo
            this.form.description = projectInfo.description.join(',')
            this.dialogVisible = true
        },
        delProject(row) {
            this.$confirm('此操作将永久删除该项目, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delProject(row.id).then(res => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    this.fetchData()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        handleEdit() {
            // console.log('编辑项目',this.form)
            let obj = { ...this.form }
            obj.description = this.form.description.split(',')
            obj.order = parseInt(this.form.order)

            setProject(obj.id,obj).then(res => {
                this.dialogVisible = false
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
                this.fetchData()
            })
        }
    }
}
</script>

<style scoped lang="scss">

</style>
